import pytest
import argparse


def main():
    """主运行函数"""
    parser = argparse.ArgumentParser(description='洛书融媒接口自动化测试')
    parser.add_argument('--env', choices=['dev', 'test', 'pre', 'prod'],
                        default='dev', help='运行环境')
    parser.add_argument('--test-type', choices=['all', 'unit', 'integration', 'monitor'],
                        default='all', help='测试类型')
    parser.add_argument('--report', action='store_true', help='生成HTML报告')

    args = parser.parse_args()

    # 设置环境变量
    import os
    os.environ['ENV'] = args.env

    # pytest参数
    pytest_args = ['-v', '--tb=short']

    # 根据测试类型选择测试目录
    if args.test_type == 'unit':
        pytest_args.extend(['tests/testcases/'])
    elif args.test_type == 'integration':
        pytest_args.extend(['tests/integration/'])
    elif args.test_type == 'monitor':
        pytest_args.extend(['tests/monitor/'])
    else:
        pytest_args.extend(['tests/'])

    # 生成报告
    if args.report:
        pytest_args.extend(['--html=reports/html/report.html', '--self-contained-html'])

    # 运行测试
    pytest.main(pytest_args)


if __name__ == '__main__':
    main()
